import WxValidate from '../../utils/wxValidate.js'
const config = require(`../../config.js`);
let api = require('../../utils/api.js')
Page({

data: {
    //手机号字段
    phone: '',
    // 错误信息
    errorText: "",
    // 禁用提交
    forbidSubmit: false,
    //发送验证码
    time: `获取验证码`,
    //倒计时
    currentTime:61,
    //验证码
    loginCode:"",
    //微信头像
    avatarUrl:'',
    //微信性别
    gender:'',
    //微信名字
    nickName:'',
    //省份
    province:'',
   // 城市
     city:'',
     code:'',
    cityId:'',
    referee:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      city: options.city,
      avatarUrl: options.avatarUrl,
      province: options.province,
      gender: options.gender,
      nickName: options.nickName,
      cityId: options.cityId,
      referee: options.referee
    })
    that.initValidate()
  },

  // 正则验证
  initValidate() {
    const rules = { 
      phone: {
        required: true,
        tel: true
      },
      loginCode: {
        required: true,
      },
    }
    const messages = {
      phone: {
        required: '请填写手机号',
        tel: '请填写正确的手机号'
      },
      loginCode: {
        required: '请填写验证码',
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },

  //获取手机号
  btnPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //获取验证码
  getCode: function (e) {
    this.setData({
      loginCode: e.detail.value
    })
  },


  //点击登陆
  formSubmit(e) {
    let that=this
    //校验表单
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }

    wx.login({
      success: rest => {
        //登陆
        wx.request({
          url: `${config.url}/admin/user/login`,
          data: {
            code: that.data.loginCode,
            wxCode: rest.code,
            phone: that.data.phone,
            city: that.data.city,
            province: that.data.province,
            imgurl: that.data.avatarUrl,
            loginType: '2',
            sex: that.data.gender,
            nickname: that.data.nickName,
            type: '1',
            cityId: that.data.cityId == 'undefined' ? '' : that.data.cityId,
            referee: that.data.referee == 'undefined' ? '' : that.data.referee,
            shopId: '-1'
          },
          header: {
            'content-type': 'application/json',
          },
          method: 'POST',
          success: function (res) {

            if (res.data.result == true) {
          
              //0是别人登陆 1是商家登陆 2个人登陆
              wx.setStorageSync("type", res.data.object.type)
              //本地存储
              wx.setStorageSync("token", res.data.data)
              wx.setStorageSync("phone", res.data.object.phone)
              //openId
              wx.setStorageSync("openId", res.data.body.openid)
              //城市Id
              //codeNum
              // wx.setStorageSync("codeNum", res.data.code)
              wx.setStorageSync("userMess", res.data.object)
              if (wx.getStorageSync("userId") != '') {
                that.empower()
              }
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 1500,
                success: () => {
                  wx.reLaunch({
                    url: '/pages/index/index',
                  })
                }
              })
            } else if (res.data.result == false) {
              if (res.data.code == 95) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
              if (res.data.code == 96) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
              //codeNum
              // wx.setStorageSync("codeNum", res.data.code)
              //本地存储
              wx.setStorageSync("token", res.data.data)
              wx.setStorageSync("userMess", res.data.object)
              //openId
              wx.setStorageSync("openId", res.data.body.openid)
              if (res.data.code == 103) {
                wx.navigateTo({
                  url: '/pages/type/type',
                })
              } else {
                wx.reLaunch({
                  url: '/pages/index/index',
                })
              }
            }
          }
        })
         
      
      }
    })
  
  },

 
  //正则验证动画 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },

  // 请求验证码
  requestCode: function () {
    let that = this;
    //获取data手机号
    let phone = that.data.phone;

    //手机号正则
    let myreg = /^1[3456789]\d{9}$/;
    if (myreg.test(phone)) {
      wx.request({
        url: `${config.url}/admin/note/login/send`,
        data: {
          phone:phone
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        method: 'POST',
        success: function (res) {
          if (res.data.result==true){
               wx.showToast({
                title: res.data.msg,
                 icon: 'none',
                 duration:1500
              })
            }
        }
      })
    } else {
      wx.showModal({
        content: '请输入正确手机号',
        showCancel: false,
      })
      return
    }

    // ---------------------------验证码倒计时---------------------------------
    //获取currentTime
    var currentTime = that.data.currentTime
    that.setData({
      disabled: true
    })
    var interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
  
  empower: function (res) {
    api.empower({
      grantUserId: wx.getStorageSync("userId"),
      userId: wx.getStorageSync("userMess").id,
    }).then(res => {
      console.log(res)
    })
  }

})